/**
 * RightPanel.java
 * 
 * Copyright (C) 2012 - Thibault BUREL
 * 
 * This file is part of MathDraft.
 *
 * MathDraft is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MathDraft is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MathDraft.  If not, see <http://www.gnu.org/licenses/>
 *  
 */
package drk.mathdraft.ihm;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import drk.mathdraft.StudiedProposition;
import drk.mathdraft.ihm.tree.ExpressionTree;
import drk.mathdraft.proposition.Proposition;
import drk.mathdraft.proposition.builder.NaturalNotation;
import drk.mathdraft.proposition.builder.SyntaxException;

public class RightPanel extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	private static final int PANEL_WIDTH = 740;
	
	protected Cell           cell;
	protected drk.mathdraft.ihm.command.CommandPanel command;
	protected JTextField     expAcquisition;
	protected JButton        firstSelection;
	protected JButton        secondSelection;
	protected JLabel         firstSelLabel;
	protected JLabel         secondSelLabel;
	protected JRadioButton   directButton;
	protected JRadioButton   inverseButton;
	protected ExpressionTree tree;

	protected Proposition    firstNode;
	protected Proposition    secondNode;
	
	public RightPanel (ExpressionTree t) {
		super();
		
		cell             = new Cell();
		command          = new drk.mathdraft.ihm.command.CommandPanel();
		firstSelection   = new JButton("First node");
		secondSelection  = new JButton("Second node");
		firstSelLabel    = new JLabel();
		secondSelLabel   = new JLabel();
		expAcquisition   = new JTextField();
		directButton     = new JRadioButton("Direct");
		inverseButton    = new JRadioButton("Inverse");
		tree             = t;
		
		cell.setBackground(Color.WHITE);
		
		new CellMouseListener (cell);
		
		
		cell.setVisible(true);
		
		cell.setPreferredSize(new Dimension    (PANEL_WIDTH,150));
		cell.setMaximumSize(new Dimension      (PANEL_WIDTH,150));
		command.setPreferredSize(new Dimension (PANEL_WIDTH,350));
		command.setMaximumSize(new Dimension   (PANEL_WIDTH,350));
		
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		cell.setAlignmentX(Component.LEFT_ALIGNMENT);
		command.setAlignmentX(Component.LEFT_ALIGNMENT);
		this.add (cell);
		this.add (command);
		this.add (createParamPanel());
		
		this.expAcquisition.addActionListener(this);
		this.firstSelection.addActionListener(this);
		this.secondSelection.addActionListener(this);
		
	}
	
	public void actionPerformed (ActionEvent e)
	{
		if(e.getSource().equals(expAcquisition))
		{
			try
			{
				StudiedProposition.getInstance().setProposition
					(NaturalNotation.fromNaturalNotation
						(expAcquisition.getText()));
			}
			catch (SyntaxException exc)
			{
				// Impossible !
			}		
			expAcquisition.setText("");
		}
		if(e.getSource().equals(firstSelection))
		{
			firstNode = tree.getSelectedNode();
			this.firstSelLabel.setText(firstNode.toString()+" "+firstNode.getParent());
		}
		if(e.getSource().equals(secondSelection))
		{
			secondNode = tree.getSelectedNode();
			this.secondSelLabel.setText(secondNode.toString());
		}
	}
	
	public JPanel createParamPanel()
	{
		int FIRST_WIDTH  = 150;
		int H_MARGIN     = 10;
		int V_MARGIN     = 5;
		int LINE_HEIGHT  = 20 + V_MARGIN;
		int SECOND_WIDTH = PANEL_WIDTH - FIRST_WIDTH - H_MARGIN;
		
		JPanel all   = new JPanel();
		all.setLayout(null);
		
		// First line
		ButtonGroup bg = new ButtonGroup();
		bg.add(this.directButton);
		bg.add(this.inverseButton);
		
		JLabel label = new JLabel("Proposition");
		
		all.add(this.directButton);
		all.add(this.inverseButton);

		all.add(label);
		all.add(this.expAcquisition);		
		
		all.add(this.firstSelection);
		all.add(this.firstSelLabel);
		
		
		all.add(this.secondSelection);
		all.add(this.secondSelLabel);
		
		// set geometry
		label.setBounds(0, 0, FIRST_WIDTH, LINE_HEIGHT - V_MARGIN);
		this.expAcquisition.setBounds(FIRST_WIDTH + H_MARGIN, 0, SECOND_WIDTH, LINE_HEIGHT - V_MARGIN);
		this.directButton.setBounds(0, LINE_HEIGHT, FIRST_WIDTH, LINE_HEIGHT - V_MARGIN);
		this.inverseButton.setBounds(FIRST_WIDTH + H_MARGIN, LINE_HEIGHT, FIRST_WIDTH, LINE_HEIGHT - V_MARGIN);
		this.firstSelection.setBounds(0, 2*LINE_HEIGHT, FIRST_WIDTH, LINE_HEIGHT - V_MARGIN);
		this.firstSelLabel.setBounds(FIRST_WIDTH + H_MARGIN,2*LINE_HEIGHT,SECOND_WIDTH,LINE_HEIGHT - V_MARGIN);
		this.secondSelection.setBounds(0, 3*LINE_HEIGHT, FIRST_WIDTH, LINE_HEIGHT - V_MARGIN);
		this.secondSelLabel.setBounds(FIRST_WIDTH + H_MARGIN,3*LINE_HEIGHT,SECOND_WIDTH,LINE_HEIGHT - V_MARGIN);
		
		// Last init
		this.directButton.setSelected(true);
		
		return all;
	}	
	
	public boolean getDirect()
	{
		return this.directButton.isSelected();
	}
	
	public Proposition getFirstNode()
	{
		return this.firstNode;
	}
	
	public Proposition getSecondtNode()
	{
		return this.secondNode;
	}
	
	public String getLineCommand()
	{
		return expAcquisition.getText();
	}
	

}